package cc.android.testapp.util;

import android.text.TextPaint;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Random;

import cc.commons.util.extra.CList;
import cc.commons.util.reflect.FieldUtil;

public class CTool {

    private static final Random mRan = new Random();

    /**
     * 基于pValue,在pRange范围内随机坐标
     *
     * @param pValue 基础坐标,119.或者29.
     * @param pRange 范围,单位是米
     * @return 随机后的坐标
     */
    public static double shakeCoordinate(double pValue, int pRange) {
        return pValue + (mRan.nextDouble() - 0.5) * 2 / (100000.0 / pRange);
    }

    public static boolean classExist(ClassLoader pLoader, String pClazz) {
        try {
            pLoader.loadClass(pClazz);
            return true;
        } catch (Throwable ignored) {
            return false;
        }
    }

    /**
     * 将实例的所有字段转换成字符串
     *
     * @param pValue 实例
     * @return 字符串
     */
    public static String fieldVToStr(Object pValue) {
        return fieldVToStr(pValue, ", ");
    }


    /**
     * 将实例的所有字段转换成字符串,每个字段值各占一行
     *
     * @param pValue 实例
     * @return 字符串
     */
    public static String fieldVToStrV(Object pValue) {
        return fieldVToStr(pValue, ",\r\n");
    }

    /**
     * 将实例的所有字段转换成字符串
     *
     * @param pValue 实例
     * @param pSplit 各个字段值的分隔符
     * @return 字符串
     */
    public static String fieldVToStr(Object pValue, String pSplit) {
        StringBuilder tSBuilder = new StringBuilder();
        CList<Field> tFields = null;
        try {
            tFields = FieldUtil.getDeclaredField(pValue.getClass(), (pField) -> !Modifier.isStatic(pField.getModifiers()));
        } catch (IllegalStateException ignored) {
            return "";
        }
        for (Field sField : tFields) {
            tSBuilder.append('"').append(sField.getName()).append('"').append(": ").append(FieldUtil.getFieldValue(sField, pValue));
            tSBuilder.append(pSplit);
        }
        return tSBuilder.toString();
    }

    /**
     * 基于pNumber,在pRange范围内正负随机
     *
     * @param pNumber 基础数值
     * @param pRange  范围
     * @return 随机后的值
     */
    public static int shakeNumberInt(int pNumber, int pRange) {
        if (pNumber == -1 || pNumber == Integer.MAX_VALUE) return pNumber;
        return pNumber + (int) Math.round((1 - mRan.nextDouble() * 2) * pRange);
    }

    /**
     * 基于pNumber,在pRange范围内正负随机
     *
     * @param pNumber 基础数值
     * @param pRange  范围
     * @return 随机后的值
     */
    public static long shakeNumberLong(long pNumber, int pRange) {
        if (pNumber == -1 || pNumber == Long.MAX_VALUE) return pNumber;
        return pNumber + Math.round((1 - mRan.nextDouble() * 2) * pRange);
    }

    public static int calculateMaxChars(String text, float pFontSize, int pWidth) {
        pWidth -= pFontSize;
        int length = text.length();
        float[] measuredWidth = new float[2];
        float tLen = 0;
        TextPaint paint = new TextPaint();
        paint.setTextSize(pFontSize);

        for (int i = length; i - 2 >= 0; i -= 2) {
            paint.getTextWidths(text, i - 2, i, measuredWidth);
            tLen += measuredWidth[0] + measuredWidth[1];
            if (tLen >= pWidth) {
                return length - i;
            }
        }

        return length;
    }

}
